package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.UserParent;
import cn.anxingongyou.anxin.entity.UserWorkerInfo;
import cn.anxingongyou.anxin.service.UserParentService;
import cn.anxingongyou.anxin.service.UserWorkerInfoService;
import cn.anxingongyou.anxin.service.WorkerRepository;
import cn.anxingongyou.anxin.utils.RedisUtil;
import cn.anxingongyou.anxin.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserWorkerInfoController {

    private final UserWorkerInfoService userWorkerInfoService;
    private final UserParentService userParentService;
    private final WorkerRepository workerRepository;
    @Autowired
    public UserWorkerInfoController(UserWorkerInfoService userWorkerInfoService, UserParentService userParentService, WorkerRepository workerRepository) {
        this.userWorkerInfoService = userWorkerInfoService;
        this.userParentService = userParentService;
        this.workerRepository = workerRepository;
    }

    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("添加求职者信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/worker/add",method = RequestMethod.POST)
    public Map addWorker(
            @RequestBody @ApiParam(value = "求职者信息", required = true) UserWorkerInfo userWorkerInfo
            ){
        userWorkerInfo.setCreated(new Date());
        userWorkerInfo.setUpdated(new Date());
        int res = userWorkerInfoService.insert(userWorkerInfo);

        workerRepository.save(userWorkerInfo);

        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("删除求职者信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")
    })
    @RequestMapping(value = "/worker/del/{id}",method = RequestMethod.GET)
    public Map delWorker(
            @PathVariable @ApiParam(value = "求职者主键", required = true) Long id
    ){
        int res = userWorkerInfoService.deleteByPrimaryKey(id);
        workerRepository.deleteById(id);
        if(res > 0) {
            return Result.instance(204,"删除成功").toJsonMap();
        } else {
            return Result.instance(500,"删除失败").toJsonMap();
        }
    }

    @ApiOperation("更新求职者信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/worker/update",method = RequestMethod.POST)
    public Map updateWorker(
            @RequestBody @ApiParam(value = "用户信息,必传主键", required = true) UserWorkerInfo userWorkerInfo
    ){
        String key = "worker#" + userWorkerInfo.getId().toString();
        if(redisUtil.hasKey(key)){
            //  存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = userWorkerInfoService.updateByPrimaryKeySelective(userWorkerInfo);
        userWorkerInfo = userWorkerInfoService.selectOneByPrimaryKey(userWorkerInfo.getId());
        workerRepository.save(userWorkerInfo);
        if(res > 0) {
            redisUtil.set(key,userWorkerInfo);
            return Result.<UserWorkerInfo>instance(204,"更新成功",userWorkerInfo).toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("个人认证通过")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/worker/isauth",method = RequestMethod.POST)
    public Map isauthWorker(
            @RequestBody @ApiParam(value = "用户信息,必传主键", required = true) UserWorkerInfo userWorkerInfo
    ){
        String key = "worker#" + userWorkerInfo.getId().toString();
        if(redisUtil.hasKey(key)){
            //  存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = userWorkerInfoService.updateByPrimaryKeySelective(userWorkerInfo);

        userWorkerInfo = userWorkerInfoService.selectOneByPrimaryKey(userWorkerInfo.getId());
        workerRepository.save(userWorkerInfo);

        UserParent userParent = new UserParent();
        userParent.setId(userWorkerInfo.getId());
        userParent.setIsPersonalAuthentication(1);
        userParentService.updateByPrimaryKeySelective(userParent);
        if(res > 0) {
            return Result.instance(204,"设置成功").toJsonMap();
        } else {
            return Result.instance(500,"操作失败").toJsonMap();
        }
    }

    @ApiOperation("个人认证未通过")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/worker/isnotauth",method = RequestMethod.POST)
    public Map isnotauthWorker(
            @RequestBody @ApiParam(value = "用户信息,必传主键", required = true) UserWorkerInfo userWorkerInfo
    ){
        String key = "worker#" + userWorkerInfo.getId().toString();
        if(redisUtil.hasKey(key)){
            //  存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = userWorkerInfoService.updateByPrimaryKeySelective(userWorkerInfo);

        userWorkerInfo = userWorkerInfoService.selectOneByPrimaryKey(userWorkerInfo.getId());
        workerRepository.save(userWorkerInfo);

        if(res > 0) {
            return Result.instance(204,"设置成功").toJsonMap();
        } else {
            return Result.instance(500,"操作失败").toJsonMap();
        }
    }

    @ApiOperation("api更新求职者信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/api/worker/update",method = RequestMethod.POST)
    public Map apiUpdateWorker(
            @RequestBody @ApiParam(value = "用户信息,必传主键", required = true) UserWorkerInfo userWorkerInfo
    ){
        String key = "worker#" + userWorkerInfo.getId().toString();
        if(redisUtil.hasKey(key)){
            //  存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = userWorkerInfoService.updateByPrimaryKeySelective(userWorkerInfo);

        userWorkerInfo = userWorkerInfoService.selectOneByPrimaryKey(userWorkerInfo.getId());
        workerRepository.save(userWorkerInfo);

        if(res > 0) {
            redisUtil.set(key,userWorkerInfo);
            return Result.<UserWorkerInfo>instance(204,"更新成功",userWorkerInfo).toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("api完善简历信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/api/worker/perfect",method = RequestMethod.POST)
    public Map apiPerfectWorker(
            @RequestBody @ApiParam(value = "用户信息,必传主键", required = true) UserWorkerInfo userWorkerInfo
    ){
        String key = "worker#" + userWorkerInfo.getId().toString();
        if(redisUtil.hasKey(key)){
            //  存在缓存 删除缓存
            redisUtil.del(key);
        }
        userWorkerInfo.setIsPerfect(1);
        int res = userWorkerInfoService.updateByPrimaryKeySelective(userWorkerInfo);
        UserParent userParent = new UserParent();
        userParent.setId(userWorkerInfo.getId());
        userParent.setIsWorkerPerfect(1);
        userParentService.updateByPrimaryKeySelective(userParent);
        userWorkerInfo = userWorkerInfoService.selectOneByPrimaryKey(userWorkerInfo.getId());
        workerRepository.save(userWorkerInfo);
        if(res > 0) {
            redisUtil.set(key,userWorkerInfo);
            return Result.<UserWorkerInfo>instance(204,"完善成功",userWorkerInfo).toJsonMap();
        } else {
            return Result.instance(500,"完善失败").toJsonMap();
        }
    }



    @ApiOperation("获取单个信息通过")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/worker/findByWorker",method = RequestMethod.POST)
    public Map findOneWorker(
            @RequestBody @ApiParam(value = "用户信息", required = true) UserWorkerInfo userWorkerInfo
    ){
        UserWorkerInfo userWorkerInfo1 = userWorkerInfoService.selectOne(userWorkerInfo);
        if(userWorkerInfo1 != null) {
            return Result.<UserWorkerInfo>instance(200,"获取成功",userWorkerInfo1).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("api获取单个信息通过")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/api/worker/findByWorker",method = RequestMethod.POST)
    public Map apiFindOneWorker(
            @RequestBody @ApiParam(value = "用户信息", required = true) UserWorkerInfo userWorkerInfo
    ){
        UserWorkerInfo userWorkerInfo1 = userWorkerInfoService.selectOne(userWorkerInfo);

        if(userWorkerInfo1 != null) {
            return Result.<UserWorkerInfo>instance(200,"获取成功",userWorkerInfo1).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取求职者信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/worker/findOneById/{id}",method = RequestMethod.GET)
    public Map findOneById(
            @PathVariable @ApiParam(value = "求职者主键", required = true) Long id
    ){
        UserWorkerInfo userWorkerInfo = userWorkerInfoService.selectOneByPrimaryKey(id);
        if(userWorkerInfo != null) {
            workerRepository.save(userWorkerInfo);
            return Result.<UserWorkerInfo>instance(200,"获取成功",userWorkerInfo).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("api获取求职者信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/api/worker/findOneById/{id}",method = RequestMethod.GET)
    public Map apiFindOneById(
            @PathVariable @ApiParam(value = "求职者主键", required = true) Long id
    ){
        String key = "worker#" + id.toString();
        if(redisUtil.hasKey(key)){
            // 存在缓存
            UserWorkerInfo userWorkerInfo = (UserWorkerInfo) redisUtil.get(key);
            if(userWorkerInfo != null) {
                return Result.<UserWorkerInfo>instance(200,"获取成功",userWorkerInfo).toJsonMap();
            } else {
                return Result.instance(500,"获取失败").toJsonMap();
            }
        } else {
            // 没有缓存
            UserWorkerInfo userWorkerInfo = userWorkerInfoService.selectOneByPrimaryKey(id);
            if(userWorkerInfo != null) {
                redisUtil.set(key,userWorkerInfo);
                return Result.<UserWorkerInfo>instance(200,"获取成功",userWorkerInfo).toJsonMap();
            } else {
                return Result.instance(500,"获取失败").toJsonMap();
            }
        }
    }

    @ApiOperation("获取所有求职者")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/worker/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectAddressByWorker(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "求职者信息", required = true)  UserWorkerInfo userWorkerInfo){
        PageInfo<UserWorkerInfo> pageInfo = userWorkerInfoService.page(pageNum, pageSize, userWorkerInfo);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("api筛选求职者")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/api/worker/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectWorkerByWorker(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "求职者信息", required = true)  UserWorkerInfo userWorkerInfo){
        PageInfo<UserWorkerInfo> pageInfo = userWorkerInfoService.page(pageNum, pageSize, userWorkerInfo);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

}
